﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Core;
using PowerPoint = Microsoft.Office.Interop.PowerPoint;
using Graph = Microsoft.Office.Interop.Graph;
using System.Runtime.InteropServices;

namespace DoVuiKinhThanh
{
    using System.Collections.ObjectModel;
    using System.Configuration;
    using System.IO;

    using DoVuiKinhThanh.Domain;

    public static class PowerpointHelper
    {
        public static void CreatePowerPointPresentation(ObservableCollection<Quiz> quizes)
        {
            var strTemplate = @"";
            var templatePath = ConfigurationManager.AppSettings[Constants.APP_SETTING_POWERPOINT_TEMPLATE];
            strTemplate = !string.IsNullOrWhiteSpace(templatePath) ? templatePath : strTemplate;

            PowerPoint.Application pptApplication = new PowerPoint.Application();
            pptApplication.Visible = MsoTriState.msoTrue;

            // Create the Presentation File            
            PowerPoint.Slides slides;
            if (File.Exists(strTemplate)) {
                var pptPresentations = pptApplication.Presentations;
                var objPres = pptPresentations.Open(strTemplate , MsoTriState.msoFalse , MsoTriState.msoTrue , MsoTriState.msoTrue);
                slides = objPres.Slides;
            }
            else 
            {
                var pptPresentation = pptApplication.Presentations.Add(MsoTriState.msoTrue);
                slides = pptPresentation.Slides;             
            }
            // Create new Slide            
            var idx = 1;
            var idxList = new List<int>();
            foreach (var quiz in quizes)
            {
                AddSlide(slides, idx, quiz, PowerPoint.PpSlideLayout.ppLayoutText);
                idxList.Add(idx);
                idx++;                
            }

            // Add Transitional effect.            
            var objSldRng = slides.Range(idxList.ToArray());
            var objSlideTransition = objSldRng.SlideShowTransition;
            objSlideTransition.AdvanceOnTime = MsoTriState.msoTrue;
            objSlideTransition.AdvanceTime = 3;
            objSlideTransition.EntryEffect = PowerPoint.PpEntryEffect.ppEffectBoxOut;

            //pptPresentation.SaveAs(@"C:\dovuikinhthanh.pptx", PowerPoint.PpSaveAsFileType.ppSaveAsDefault, MsoTriState.msoTrue);
            //pptPresentation.Close();
            pptApplication.Quit();
        }

        private static void AddSlide(PowerPoint.Slides slides, int idx, Quiz quiz, PowerPoint.PpSlideLayout layout)
        {
            PowerPoint._Slide slide = slides.Add(idx, layout);         

            // Add quiz content
            PowerPoint.TextRange objText = slide.Shapes[1].TextFrame.TextRange;
            objText.Text = quiz.ContentWithoutFormat;
            objText.Font.Name = "Arial";
            objText.Font.Size = 32;

            // Add answers
            StringBuilder answerBuilder = new StringBuilder();
            foreach (var answer in quiz.Answers)
            {
                answerBuilder.AppendLine(answer.Content);
            }
            objText = slide.Shapes[2].TextFrame.TextRange;
            objText.Text = answerBuilder.ToString();

            var correctAnswer = quiz.Answers.FirstOrDefault(ans => ans.IsCorrectAnswer);
            if (correctAnswer != null)
            {
                slide.NotesPage.Shapes[2].TextFrame.TextRange.Text = correctAnswer.Content;
            }
        }

        public static void ShowPresentation2()
        {
            String strTemplate, strPic;
            strTemplate = @"C:\Program Files\Microsoft Office\Templates\1033\Pitchbook.potx";
            strPic = @"D:\Hinh\Backup\Images\beegau.jpg";
            bool bAssistantOn;
            PowerPoint.Application objApp;
            PowerPoint.Presentations objPresSet;
            PowerPoint._Presentation objPres;
            PowerPoint.Slides objSlides;
            PowerPoint._Slide objSlide;
            PowerPoint.TextRange objTextRng;
            PowerPoint.Shapes objShapes;
            PowerPoint.Shape objShape;
            PowerPoint.SlideShowWindows objSSWs;
            PowerPoint.SlideShowTransition objSST;
            PowerPoint.SlideShowSettings objSSS;
            PowerPoint.SlideRange objSldRng;
            Graph.Chart objChart;

            //Create a new presentation based on a template.
            objApp = new PowerPoint.Application();
            objApp.Visible = MsoTriState.msoTrue;
            objPresSet = objApp.Presentations;
            objPres = objPresSet.Open(strTemplate,
                MsoTriState.msoFalse, MsoTriState.msoTrue, MsoTriState.msoTrue);
            objSlides = objPres.Slides;

            //Build Slide #1:
            //Add text to the slide, change the font and insert/position a
            //picture on the first slide.
            objSlide = objSlides.Add(1, PowerPoint.PpSlideLayout.ppLayoutTitleOnly);
            objTextRng = objSlide.Shapes[1].TextFrame.TextRange;
            objTextRng.Text = "My Sample Presentation";
            objTextRng.Font.Name = "Comic Sans MS";
            objTextRng.Font.Size = 48;
            objSlide.Shapes.AddPicture(strPic, MsoTriState.msoFalse, MsoTriState.msoTrue,
                150, 150, 500, 350);

            //Build Slide #2:
            //Add text to the slide title, format the text. Also add a chart to the
            //slide and change the chart type to a 3D pie chart.
            objSlide = objSlides.Add(2, PowerPoint.PpSlideLayout.ppLayoutTitleOnly);
            objTextRng = objSlide.Shapes[1].TextFrame.TextRange;
            objTextRng.Text = "My Chart";
            objTextRng.Font.Name = "Comic Sans MS";
            objTextRng.Font.Size = 48;
            objChart = (Graph.Chart)objSlide.Shapes.AddOLEObject(150, 150, 480, 320,
                "MSGraph.Chart.8", "", MsoTriState.msoFalse, "", 0, "",
                MsoTriState.msoFalse).OLEFormat.Object;
            objChart.ChartType = Graph.XlChartType.xl3DPie;
            objChart.Legend.Position = Graph.XlLegendPosition.xlLegendPositionBottom;
            objChart.HasTitle = true;
            objChart.ChartTitle.Text = "Here it is...";

            //Build Slide #3:
            //Change the background color of this slide only. Add a text effect to the slide
            //and apply various color schemes and shadows to the text effect.
            objSlide = objSlides.Add(3, PowerPoint.PpSlideLayout.ppLayoutBlank);
            objSlide.FollowMasterBackground = MsoTriState.msoFalse;
            objShapes = objSlide.Shapes;
            objShape = objShapes.AddTextEffect(MsoPresetTextEffect.msoTextEffect27,
                "The End", "Impact", 96, MsoTriState.msoFalse, MsoTriState.msoFalse, 230, 200);

            //Modify the slide show transition settings for all 3 slides in
            //the presentation.
            int[] SlideIdx = new int[3];
            for (int i = 0; i < 3; i++) SlideIdx[i] = i + 1;
            objSldRng = objSlides.Range(SlideIdx);
            objSST = objSldRng.SlideShowTransition;
            objSST.AdvanceOnTime = MsoTriState.msoTrue;
            objSST.AdvanceTime = 3;
            objSST.EntryEffect = PowerPoint.PpEntryEffect.ppEffectBoxOut;

            //Prevent Office Assistant from displaying alert messages:
            bAssistantOn = objApp.Assistant.On;
            objApp.Assistant.On = false;

            //Run the Slide show from slides 1 thru 3.
            objSSS = objPres.SlideShowSettings;
            objSSS.StartingSlide = 1;
            objSSS.EndingSlide = 3;
            objSSS.Run();

            //Wait for the slide show to end.
            objSSWs = objApp.SlideShowWindows;
            while (objSSWs.Count >= 1) System.Threading.Thread.Sleep(100);

            //Reenable Office Assisant, if it was on:
            if (bAssistantOn)
            {
                objApp.Assistant.On = true;
                objApp.Assistant.Visible = false;
            }

            //Close the presentation without saving changes and quit PowerPoint.
            objPres.Close();
            objApp.Quit();
        }
    }
}
